<?php
// +----------------------------------------------------------------------
// | 奇乐中介担保交易系统 免费版 2019
// +----------------------------------------------------------------------
// | 官方网址：http://www.qilecms.com   
// +----------------------------------------------------------------------
// | 产品文档：http://www.qilecms.com/doc
// +----------------------------------------------------------------------
// | 开发作者:奇乐CMS
// +----------------------------------------------------------------------
// | 版权说明：这不是一个自由软件！本产品仅限个人使用，但请务必保留底部版权，以免产生不必要的纠纷,
// | 您只能在不用于商业目的的前提下对程序代码进行修改和使用，
// | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
// +----------------------------------------------------------------------
namespace app\user\controller;
//用户注册
class Register extends Base{

    public function index() {

     // 注册页面
       if($this->request->isAjax()){
           $param = $this->request->param();
          
          if($param['type']  == 'account'){
           // 关闭账号注册
             return $this->accountRegister($param);
          
          }else{
              return error_json('抱歉，请求方式错误');
          }

       }else{
       	 return $this->fetch();
       }
       
    }
    protected function accountRegister($param)
{
    //用户名
           $rule =[
            'username'   =>'require|min:3|max:15|alphaDash',
              'password'   =>'require|min:6',
              'verify_code'=>'require',
            ];
            $msg= [
               'username.require' =>'请输入用户名',
               'username.min'     =>'用户名至少3个字符',
               'username.max'     =>'用户名最多15个字符',
               'username.alphaDash' =>'用户名只能是字母和数字，下划线_及破折号',

             'password.require' =>'请输入登陆密码',
             'password.min'      => '密码至少6位数',
             'verify_code.require' =>'请输入验证码',
            
            ];
             $error = $this->checkSubmit($param,$rule,$msg);  
            if($error){
                return error_json($error);
             } 


              
           //判断验证码是否正确
            if(!captcha_check($param['verify_code'])){
            return error_json('验证码错误');
            };
           //检测用户是否为纯数字
           if(preg_match("/^(?!(\d+)$)[a-zA-Z0-9_\x{4e00}-\x{9fa5}]{3,15}$/u",$param['username'])){
                   
       //匹配用户名
                      $where['username']  = $param['username'];
                      $data['username']   = $param['username'];

            }else{
                     return error_json('用户名仅支持中英文、数字和下划线');
            }
          
         //过滤非法用户名
            $keywords_ban = $this->settings['site']['register_keywords_ban'];
            if(!empty($keywords_ban)){
               $keywords_ban_data = explode('|',$keywords_ban); 
               if(is_array($keywords_ban_data)){
                  if(in_array($param['username'],$keywords_ban_data )){
                     return error_json('当前用户名禁止注册，请更换其他用户名');
                   }
               }     
          
            }



            $count = model('user')->where($where)->count();
            if(!empty($count)){
                return error_json('抱歉，该用户名已经注册，请更换其他账号');
             } 
            
     
           //判断审核方式    
           if(!empty($this->settings['site']['new_register']) &&  $this->settings['site']['new_register'] ==1){
            //人工审核
         $data['status']  = 0;
      }else{
         //自动审核 
         $data['status']  = 1;
      }
                      
             $data['password']  = $param['password'];
             $passsalt          = random_text(8);
   
             $data['nickname']  = $data['username']; //昵称
             $data['password']  = password_key($data['username'],$param['password'],$passsalt);
             $data['passsalt']  = $passsalt;
             $paysalt           = random_text(8);
             $data['payword']   = payword_key($data['username'],$param['password'],$paysalt);
             $data['paysalt']   = $paysalt;
             $data['login_count'] = 1;
             $data['login_time']  = time();
             $data['login_ip']    = get_client_ip();

             $data['reg_time']    = time();
             $data['reg_ip']      = get_client_ip();

             $user = model('user')->addData($data);
             session('uid',$user->id);
             session('username',$user->username);
             //获得会员组
             $uid= $user->id;
             $res = model('user')->alias('a')->join('user_level b','b.level_id = a.level_id','LEFT')->where("a.uid = '$uid'")->find();
             session('level_id',$res['level_id']);
             session('level_name',$res['level_name']);
             return success_json('恭喜您，注册的成功');

}




}